package com.roc.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import com.converter.pack.JBigInflateConverter;
import com.roc.util.Utils;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import sun.misc.BASE64Encoder;

public class Jbigtobmp {
	
	
	//银联电子签名,jbig压缩格式转为可见格式(如bmp,jpg等)。使用方式如下
	public static void main(String[] args) {
			String data="0000000100000001400000008A000000030800031CFF02FF02FF02FF02FF02FF02FF02FF02E404F1A61640FF022D317383B9F5B095BFAD31FF021A4ED3267518C507F4FF0223D43FACAD0284FF023EB1200DFE1080FF020124142AFDFF026529AAA032BC6B3B90FF023A7365C7E920966FE74DDEFF025DB3EB894C6E9B0492C6FF02175D724D3680FF02B2BD8B9EBA2D58ADE6644FDAFF023DCE605BDB257D920249650AD217A7EA64311CA0FF024415885AE9D24040366C8F9D086CB36B5DD1C39B66D49BBEAB0A40FF0227D743676393550936EBB47AF646AB641D8860FF0240AE7CA6945C03F3BC880986DE9DDCD848FF020E108AA5462574AA5A9C1A04AB4EAB245F9E34AD9C5868FF024AF10CBE4F740D8F02960C5EFF0298FC11628516E8C52068D2CDD180FF0224559ECE4DA6D5DB592747C617A26F80FF02382E50B7D0564E3687DD97CE20FF025A810253DB089EEF0D64FF02A80F12452BDA4AC0FF024D0C14ADF3B482EDE6DCFF02B1F3CD90B4213CDCEB8D8080FF02C18B8A26DB5D6FF6DFC0844448FF02CD0121302361B9FEC6E098B4FF02C728FF009834237C15B4DCD8C0FF029E54848700B4902B6FEDA8FF02D57B35178AFC13D59E99C3A0FF02D588D1AFD900320D73DF4424FF02501DE088F7D0D0C3C89C8E80FF02E725F97D930564B3E89CFF02BAC06BF82A094917FF029396CB80FF02B208FF02FF02FF02FF";//签名域
			String remotePath = Utils.getConfigValue("FILE_PATH_IMG");
			//		createImg(data, remotePath+"\\77");
			createImg(data, "X:\\imgSave\\img\\55");
			
		}
		
		public  static  void createImg(String data, String fileName) {
			try {
			File file = new File(fileName + ".jbig");
			FileOutputStream fis = new FileOutputStream(file);
			
			fis.write(hexStringToBytes(data));//16进制转为byte数组,网上方法很多
			fis.flush();
			fis.close();
			JBigInflateConverter jic = new JBigInflateConverter();//调用jar包里面的方法
			 jic.DoConvert(fileName+".jbig", fileName+".bmp");
			} catch (Exception e) {
			e.printStackTrace();
			
			}
		}   
		
		public static byte[] hexStringToBytes(String hexString) throws UnsupportedEncodingException {   
		    if (hexString == null || hexString.equals("")) {   
		        return null;   
		    }   
		    hexString = hexString.toUpperCase();   
		    int length = hexString.length() / 2;   
		    char[] hexChars = hexString.toCharArray();   
		    byte[] d = new byte[length];   
		    for (int i = 0; i < length; i++) {   
		        int pos = i * 2;   
		        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));   
		    }   
		    
		    
		    String s = byte2Base64StringFun(d);
		  
		    return d;   
		    
		}   
		
		
		
		  public static String byte2Base64StringFun(byte[] b){
			  
			 return Base64.encode(b);
//		        return Base64.encodeBase64String(b);  
		    }  
		
		 private static byte charToByte(char c) {   
			    return (byte) "0123456789ABCDEF".indexOf(c);   
			}  

		 
		 
		 /**
		  * @Description: 根据图片地址转换为base64编码字符串
		  * @Author: 
		  * @CreateTime: 
		  * @return
		  */
		 public static String getImageStr(String imgFile) {
		     InputStream inputStream = null;
		     byte[] data = null;
		     try {
		         inputStream = new FileInputStream(imgFile);
		         data = new byte[inputStream.available()];
		         inputStream.read(data);
		         inputStream.close();
		     } catch (IOException e) {
		         e.printStackTrace();
		     }
		     // 加密
		     BASE64Encoder encoder = new BASE64Encoder();
		     return encoder.encode(data);
		 }
		 
		 
		 
		 public static int  judeFileExists(File file) {
			 if(file.exists()) {
				 return  1 ;
			 }
			 else return 0;
		 }
		 
}
